package data;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class POS extends AbstractData {
	private long id = -1;
	private transient long hotelId;
	private String Title;
	private String Description;
	private String pospointId;
	private boolean product;
	private boolean table;
	private boolean status;
	private boolean avaliable;
	private Counter defaultCounter;
	private String defaultCounterString;
	private List<Counter> counter;
	private String counterString;
	private List<POSTax> listPOSTax;
	private String listPOSTaxString;

	public POS() {
		status = true;
		avaliable = true;
	}

	public POS(long id) {
		this.id = id;
		this.status = true;
		this.avaliable = true;
	}
	
	@Override
	public long getId() {
		return id;
	}

	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public void setId(long id) {
		this.id = id;
	}
	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isAvaliable() {
		return avaliable;
	}

	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}
	
	public String getTitle() {
		return Title;
	}

	public void setTitle(String title) {
		Title = title;
	}
	
	public String getDescription() {
		return Description;
	}

	public void setDescription(String description) {
		Description = description;
	}
	
	public String getPospointId() {
		return pospointId;
	}

	public void setPospointId(String pospointId) {
		this.pospointId = pospointId;
	}
	
	public boolean isProduct() {
		return product;
	}

	public void setProduct(boolean product) {
		this.product = product;
	}

	public boolean isTable() {
		return table;
	}

	public void setTable(boolean table) {
		this.table = table;
	}
	
	public Counter getDefaultCounter() {
		if (defaultCounter == null
				&& !(defaultCounterString == null || defaultCounterString.isEmpty())) {
			Counter p = new Counter(Long.parseLong(defaultCounterString));
			p.LoadData();
			defaultCounter = p;
		}
		return defaultCounter;
	}
	public void setDefaultCounter(Counter defaultCounter) {
		this.defaultCounterString = String.format("%d", defaultCounter.getId());
		this.defaultCounter = defaultCounter;
	}
	
	public List<Counter> getCounter() {
		if ((counter == null || counter.size() == 0) && !(counterString == null || counterString.isEmpty())) {
			String[] idOfCounter = SplitList(counterString);
			List<Counter> list = new ArrayList<Counter>();
			if(idOfCounter != null)
				for (String idCounter : idOfCounter) {
					Counter c = new Counter(Long.parseLong(idCounter));
					list.add(c);
				}
			
				for(Counter c : list)
					c.LoadData();
			
			counter = list;
		}
			return counter;
	}
	public void setCounter(List<Counter> counter) {
		this.counterString = CombineList(counter);
		this.counter = counter;
	}
	
	public List<POSTax> getListPOSTax() {
		if ((listPOSTax == null || listPOSTax.size() == 0) && !(listPOSTaxString == null || listPOSTaxString.isEmpty())) {
			String[] idOfPOSTax = SplitList(listPOSTaxString);
			List<POSTax> list = new ArrayList<POSTax>();
			if(idOfPOSTax != null)
				for (String idPOSTax : idOfPOSTax) {
					POSTax c = new POSTax(Long.parseLong(idPOSTax));
					list.add(c);
				}
			
				for(POSTax c : list)
					c.LoadData();
			
				listPOSTax = list;
		}
			return listPOSTax;
	}

	public void setListPOSTax(List<POSTax> listPOSTax) {
		this.listPOSTaxString = CombineList(listPOSTax);
		this.listPOSTax = listPOSTax;
	}
	
	public String getListPOSTaxString() {
		return listPOSTaxString;
	}

	public void setListPOSTaxString(String listPOSTaxString) {
		this.listPOSTaxString = listPOSTaxString;
	}
	
	public String getDefaultCounterString() {
		return defaultCounterString;
	}

	public void setDefaultCounterString(String defaultCounterString) {
		this.defaultCounterString = defaultCounterString;
	}

	public String getCounterString() {
		return counterString;
	}

	public void setCounterString(String counterString) {
		this.counterString = counterString;
	}

	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.POS);
		else
			item = new Entity(this.key);
		item.setProperty("Title", this.Title);
		item.setProperty("Description", this.Description);
		item.setProperty("pospointId", this.pospointId);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("product", this.product);
		item.setProperty("table", this.table);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);
		item.setProperty("counter", this.counterString);
		item.setProperty("listPOSTaxString", this.listPOSTaxString);
		item.setProperty("defaultCounter", this.defaultCounterString);
		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.Title=(String)entity.getProperty("Title");
		this.Description=(String)entity.getProperty("Description");
		this.pospointId=(String)entity.getProperty("pospointId");
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.product = (Boolean) entity.getProperty("product");
		this.table = (Boolean) entity.getProperty("table");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		this.status = (Boolean) entity.getProperty("status");
		this.counterString = (String) entity.getProperty("counter");
		this.listPOSTaxString = (String) entity.getProperty("listPOSTaxString");
		this.defaultCounterString = (String) entity.getProperty("defaultCounter");
		this.key = entity.getKey();
	}

	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.POS, this.id);
			setEntity(entity);
		}
	}

}

